/*
 * File Name:               CTimeManager.cs
 *
 * Description:             时间相关对外接口类
 * Author:                  lisiyu <576603306@qq.com>
 * Create Date:             2016/11/18
 */

using System;
using UnityEngine;

public class XTimeManager : SingletonTemplate<XTimeManager>
{
    private XLocalTimer mScaleTimer;
    private XLocalTimer mUnscaleTimer;
    private XServerTimer mServerTimer;

    float _recordTimeScale = 1f;
    float _timeScale = 1f;

    public float RecordTimeScale
    {
        get
        {
            return _recordTimeScale;
        }
        set
        {
            _recordTimeScale = value;

            if (!_pause)
                Time.timeScale = _recordTimeScale * _timeScale;
        }
    }

    public float TimeScale
    {
        get
        {
            return _timeScale;
        }
        set
        {
            _timeScale = value;

            if (!_pause)
                Time.timeScale = _recordTimeScale * _timeScale;
        }
    }

    bool _pause = false;
    public bool Pause
    {
        set
        {
            if (value)
                Time.timeScale = 0f;
            else
                Time.timeScale = _recordTimeScale * _timeScale;

            _pause = value;
        }

        get
        {
            return _pause;
        }
    }

    private ulong mUidIndex;

    public XTimeManager()
    {
        mScaleTimer = new XLocalTimer(256);
        mUnscaleTimer = new XLocalTimer(256);
        mServerTimer = new XServerTimer();
        mUidIndex = 0;
    }

    public void onUpdate()
    {
        mScaleTimer.onUpdate(Time.deltaTime);
        mUnscaleTimer.onUpdate(Time.unscaledDeltaTime);
    }

    public void setServerTime(long value)
    {
        mServerTimer.setServerTime(value);
    }

    public long getServerTime()
    {
        return mServerTimer.getServerTime();
    }

    public ulong delayCall(float delayTime, Action<object> callback, object args = null)
    {
        var uid = getUidValue(0);
        mScaleTimer.delayCall(uid, delayTime, callback, args);
        return uid;
    }

    public ulong intervalCall(float intervalTime, Action<object> callback, object args = null)
    {
        var uid = getUidValue(0);
        mScaleTimer.intervalCall(uid, intervalTime, callback, args);
        return uid;
    }

    public ulong delayCallUnscale(float delayTime, Action<object> callback, object args = null)
    {
        var uid = getUidValue(1);
        mUnscaleTimer.delayCall(uid, delayTime, callback, args);
        return uid;
    }

    public ulong intervalCallUnscale(float intervalTime, Action<object> callback, object args = null)
    {
        var uid = getUidValue(1);
        mUnscaleTimer.intervalCall(uid, intervalTime, callback, args);
        return uid;
    }

    public void cancelCall(ulong uid)
    {
        if (getUidType(uid) == 0)
        {
            mScaleTimer.cancelCall(uid);
        }
        else
        {
            mUnscaleTimer.cancelCall(uid);
        }
    }

    /// <summary>
    /// 假定type属于 0-9 
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    private ulong getUidValue(uint type)
    {
        return type + mUidIndex++ * 10;
    }

    private uint getUidType(ulong value)
    {
        return (uint)(value % 10);
    }
}